package com.atguigu.PracticalProject.app;
import com.atguigu.PracticalProject.util.FlinkSourceUtil;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.state.hashmap.HashMapStateBackend;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.CheckpointConfig;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public abstract class BaseAppV1 {
    public void init(int port, int p, String ckPathGroupIdJobName, String topic) {
        System.setProperty("HADOOP_USER_NAME", "atguigu");
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", port); //设置端口号
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(p);  //设置并行度

        /*env.enableCheckpointing(3000);
        env.setStateBackend(new HashMapStateBackend());
        env.getCheckpointConfig().setCheckpointStorage("hdfs://hadoop162:8020/gmall/" + ckPathGroupIdJobName); //在hdfs存一个目录
        env.getCheckpointConfig().setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE); //设置严格一次模式
        env.getCheckpointConfig().setCheckpointTimeout(20*1000); //设置超时时间
        env.getCheckpointConfig().setMaxConcurrentCheckpoints(1); //最大并发
        env.getCheckpointConfig().setMinPauseBetweenCheckpoints(500);  //最小间隔
        env.getCheckpointConfig().setExternalizedCheckpointCleanup(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);//程序停止后设置Checkpoint保留*/

        DataStreamSource<String> stream = env.addSource(FlinkSourceUtil.getKafkaSource(ckPathGroupIdJobName, topic));

        handle(env,stream);

        try {
            env.execute(ckPathGroupIdJobName);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    protected abstract void handle(StreamExecutionEnvironment env,
                        DataStreamSource<String> stream);
}
